package com.kingyun.gpsinspection.purificationservice.services.jsm.innerservice.impl.jobpost;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cqhilink.api.common.utils.UniqueUtil;
import com.kingyun.gpsinspection.purificationservice.common.utils.BootstrapUtil;
import com.kingyun.gpsinspection.purificationservice.dao.mapper.jsm.PostMapper;
import com.kingyun.gpsinspection.purificationservice.facade.model.BootstrapSearchBean;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Employee;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Post;
import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.postVo;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.employee.JsmEmployeeService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.file.JsmFileService;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.post.JsmPostService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * Created by zmling on 2016/11/16.
 * 岗位信息
 */

public class JsmPostServiceImpl implements JsmPostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private JsmEmployeeService employeeService;
    @Autowired
    private JsmFileService fileService;

    /**
     * 查询列表
     *
     * @param bootstrapSearchBean
     * @return
     * @throws Exception
     */
    @Override
    public JSONObject findPostByDepId(BootstrapSearchBean bootstrapSearchBean) throws Exception {
        JSONObject jsonObject = new JSONObject();
        bootstrapSearchBean.setLimit(bootstrapSearchBean.getLimit() == null ? 10 : bootstrapSearchBean.getLimit());
        bootstrapSearchBean.setOffset(bootstrapSearchBean.getOffset() == null ? 0 : bootstrapSearchBean.getOffset());
        bootstrapSearchBean.setFilterMap(BootstrapUtil.filterStringToMap(bootstrapSearchBean.getFilter()));
        if (bootstrapSearchBean.getSearch() != null && bootstrapSearchBean.getSearch() != "") {
            bootstrapSearchBean.setSearch(bootstrapSearchBean.getSearch().trim());
        }
        jsonObject.put("total", postMapper.queryCount(bootstrapSearchBean));
        List<postVo> list = findList(bootstrapSearchBean);
        jsonObject.put("rows", JSON.toJSON(list));
        return jsonObject;
    }

    public List<postVo> findList(BootstrapSearchBean bootstrapSearchBean) {
        List<postVo> list = postMapper.queryList(bootstrapSearchBean);
        if (list.size() > 0) {
            for (postVo li : list) {
                if (li.getDepName() == null && li.getOrgName() != null && li.getOrgName() != "") {
                    li.setDepName(li.getOrgName());
                }
            }
        }
        return list;
    }

    /**
     * 新增
     *
     * @param post
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int addPost(Post post, String uploadFiles, String deletedFiles, Employee employee) throws Exception {
        int b = 0;
        // 批量新增岗位组织机构下去
        if (post.getPostName() != null) {
            List<String> list = JSON.parseArray(post.getPostName(), String.class);
            for (String postName : list) {
                post.setPostId(UniqueUtil.uuid());
                post.setCreatorId(employee.getEmployeeId());
                post.setPostName(postName);
                //状态0是正常；1是删除
                post.setStatus(BigDecimal.valueOf(0));
                post.setCreateTime(new Date());
                b = +postMapper.insertSelective(post);
                b = +fileService.addFile(uploadFiles, deletedFiles, post.getPostId());
            }
        }


        return b;
    }

    /**
     * 删除
     *
     * @param certificate
     * @return
     */
    @Override
    public int deletePost(String certificate) {
        int i = 0;
        List<String> list = JSONObject.parseArray(certificate, String.class);
        if (list != null) {
            int num = employeeService.selectByPostCount(list);
            if (num > 0) {
                return i;
            }
            i = postMapper.updatePostByPostId(list);
        }
        return i;
    }


    /**
     * 根据主键查询
     *
     * @param postId
     * @return
     * @throws Exception
     */
    @Override
    public postVo toPostView(String postId) throws Exception {
        return postMapper.selectByPrimaryKey(postId);
    }

    /**
     * 修改
     *
     * @param post
     * @return
     * @throws Exception
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public int updatePost(Post post, String uploadFiles, String deletedFiles, Employee employee) throws Exception {
        post.setModifierId(employee.getEmployeeId());
        post.setModifyTime(new Date());
        int b = postMapper.updateByPrimaryKeySelective(post);
        int a = fileService.updateFile(uploadFiles, deletedFiles, post.getPostId());
        return b;
    }

    /**
     * 查询
     *
     * @param depId
     * @return
     * @throws Exception
     */
    @Override
    public List<postVo> findPostDepId(String depId) throws Exception {
        return postMapper.findPostByDepId(depId);
    }

}
